<template>
  <div class="center">
    <!-- 头部 -->
    <div class="table_header flex flex_bet flex-wrap">
      <div class="table_button">
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="Refresh"></el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="addFormVisible = true">添加</el-button>
      </div>
      <div class="table_searchs flex felx_align">
        <div class="table_search flex">
          <el-input placeholder="搜索" v-model="searchTable" class="input-with-select" size="small">
            <el-select v-model="select" slot="prepend" placeholder="请选择" class="table_select" style="width:105px">
              <el-option label="批次" value="1"></el-option>
              <el-option label="子账号" value="2"></el-option>
              <el-option label="创建者" value="3"></el-option>
            </el-select>
            <el-button slot="append" icon="el-icon-search" @click="idnameSearch"></el-button>
          </el-input>
        </div>
        <dynamicTable :checkboxVal="checkboxVal" @alter="alterCheckbok" />
      </div>
    </div>

    <!-- table -->
    <el-table stripe style="width: 100%" :data="tableData" :key="key" border fit highlight-current-row ref="tableDataRef">
      <el-table-column v-if="colData[0].istrue" min-width="140" align="center" prop="id" label="ID"></el-table-column>

      <el-table-column v-if="colData[1].istrue"  min-width="100" align="center" prop="batch" label="分发批次">
        <template slot-scope="scope">
          <p v-for="(item,index) in scope.row.batch" :key="item.id" style="margin: 0;padding: 0">{{item.batch}}</p>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[2].istrue" min-width="80" align="center" prop="operates" label="分发子账号">
        <template slot-scope="scope">
          <p v-for="(item,index) in scope.row.operates" :key="item.id"  style="margin: 0;padding: 0">{{item.username}}</p>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[3].istrue" prop="create_time" label="加载中可进入桌面" min-width="140" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.is_need==1">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[4].istrue"  min-width="100" align="center" prop="admin_name" label="创建者"></el-table-column>
      <el-table-column v-if="colData[5].istrue" prop="create_time" label="创建时间" min-width="140" align="center">
        <template slot-scope="scope">{{scope.row.create_time| activateTime}}</template>
      </el-table-column>
      <el-table-column label="操作" min-width="150" align="center">
        <template slot-scope="scope">
          <el-row>
            <el-button size="mini" type="primary" @click="openEdit(scope.row)">编辑</el-button>
            <el-button size="mini" @click="deleteShow(scope.row.id)">删除</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination :current-page.sync="listQuery.startSize" background @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 50, 100, 200]" layout="total, sizes, prev, pager, next, jumper" :total="total" :page-size="listQuery.pageSize">
      </el-pagination>
    </div>

    <!-- 添加弹窗 -->
    <el-dialog title="引导页设置" :visible.sync="addFormVisible" :close-on-click-modal="false" :close-on-press-escape="false" width="25%" @close="addFormclose">
      <el-form :model="addForm" :rules="rules" ref="ruleForm" class="diolog_adapt">
        <el-form-item label="分发方式:" label-width="130px" prop="send_type">
          <el-radio-group v-model="addForm.send_type" @input="toSetBatchAdmin">
            <el-radio :label="1">批次</el-radio>
            <el-radio :label="2">子账号</el-radio>
            <el-radio :label="3">批次和子账号</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="批次:"  label-width="130px" prop="batch" v-show="addForm.send_type==1 || addForm.send_type==3">
          <el-select v-model="addForm.batch"  multiple filterable placeholder="请选择批次">
            <el-option
              v-for="item in batchArr"
              :key="item.id"
              :label="item.batch"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="子账号:" v-show="addForm.send_type==2 || addForm.send_type==3" prop="childAdmin" label-width="130px">
          <el-select v-model="addForm.childAdmin"  multiple placeholder="请选择子账号">
            <el-option
              v-for="item in childrenOption"
              :key="item.id"
              :label="item.username"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="加载中可进入桌面:" label-width="130px">
          <el-radio-group v-model="addForm.is_need">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="-1">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addFormVisible = false" size="mini" round>取 消</el-button>
        <el-button v-if="editSubjects" type="primary" @click="editGuidePage('ruleForm')" size="mini" round>确定</el-button>
        <el-button v-else type="primary" size="mini" round @click="setUpGuidePage('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>
    <!--添加弹窗 -->

    <!-- 删除弹窗start -->
    <delete-Dialog title="" :visible.sync="deleteDialogs.show" width="25%" top="40vh" @deleteEvents="deleteEvents" :deleteText="deleteDialogs.deleteText" />
    <!-- 删除弹窗end -->
  </div>
</template>

<script>
  import UploadExcelComponent from "@/components/UploadExcelSn/index.vue";
  import dynamicTable from "@/components/DynamicTable";
  import { getBatchTwo } from "@/api/equipment.js";

  import deleteDialog from "@/components/deleteDialog";
  import tableMini from '@/utils/tableMini.js'
  import {
    delGuidePage,
    editGuidePage,
    getChildAdmin,
    queryGuidePage,
    setUpGuidePage
  } from "@/api/launcherMade";
  export default {
    mixins: [tableMini],
    components: { UploadExcelComponent, dynamicTable, deleteDialog },
    data() {
      return {
        checkboxVal: ["ID", "批次", "子账号", "加载中可进入桌面", "创建者", "创建时间"],
        tableData: [],
        select: "", //搜索条件
        //编辑按钮控制
        editSubjects: false,
        listQuery: { startSize: 1, pageSize: 10, batch: undefined, username: undefined, childAdmin: undefined, userId: localStorage.getItem("ID") },
        addFormVisible: false,
        addForm: {
          send_type: 1,
          batch: [],
          child_admin: [],
          is_need: -1,  //加载中可进入桌面 1：是  -1：否
        },
        rules: {
          send_type: [{ required: true, message: "请选择分发方式", trigger: "blur" }],
          is_need: [{ required: true, message: "请设置", trigger: "blur" }],
        },
        batchArr: [],
        childrenOption: [],  //子账号可选框
      }
    },
    created() {
      this.getTabularData()
      this.getBatchTwo()
      this.getChildAdmin()
    },
    methods: {
      //删除按钮事件
      deleteEvents() {
        let params = { userId: localStorage.getItem("ID"), last_ip: sessionStorage.getItem("Ip"), id: this.deleteDialogs.deleteId }
        delGuidePage(params).then(res => {
          this.$message.success(res.msg)
          this.getTabularData()
        }).catch(error => {
          this.getTabularData()
        })
      },
      //添加
      setUpGuidePage(formName) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            let addData = JSON.parse(JSON.stringify(this.addForm))
            addData.userId = localStorage.getItem("ID")
            setUpGuidePage(addData).then(res => {
              this.$message.success(res.msg)
              this.getTabularData()
              this.addFormVisible = false
            }).catch(err => {
              this.getTabularData()
            })
          } else {
            return false;
          }
        });
      },
      //ID查询信息
      openEdit(row) {
        this.editSubjects=true
        this.addFormVisible=true
        this.addForm = {
          id: row.id,
          send_type: row.send_type,
          batch: row.batchArr,
          child_admin: row.childAdmn,
          is_need: row.is_need,  //加载中可进入桌面 1：是  -1：否
        }
      },

      //编辑信息
      editGuidePage(formName) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            let addForm = JSON.parse(JSON.stringify(this.addForm))
            addForm.userId = localStorage.getItem("ID")
            editGuidePage(addForm).then(res => {
              this.$message.success(res.msg)
              this.getTabularData()
              this.addFormVisible = false
            }).catch(err => {
            })
          } else {
            return false;
          }
        });
      },
      toSetBatchAdmin(event) {
        switch (event) {
          case 1:
            this.addForm.child_admin = [];
            break;
          case 2:
            this.addForm.batch = [];
            break;
        }
      },
      getBatchTwo() {
        getBatchTwo({ userId: localStorage.getItem("ID") }).then(res => {
          this.batchArr = res.data
        }).catch(err => {
          this.batchArr = []
        })
      },
      getChildAdmin() {
        getChildAdmin({ userId: localStorage.getItem("ID") }).then(res => {
          this.childrenOption = res.data
        }).catch(err => {
          this.childrenOption = []
        })
      },
      getTabularData() {
        queryGuidePage(this.listQuery).then(res => {
          this.tableData = res.data
          this.total = res.total
        }).catch(err => {
          this.tableData = []
          this.total = 0
        })
      },
      //搜索,1:课程名称
      idnameSearch() {
        if (!this.select) return this.$message.warning("请先选择查询条件");
        this.listQuery = this.$options.data().listQuery;
        switch (this.select) {
          case '1':
            this.listQuery.batch = this.searchTable
            this.getTabularData();
            break;
          case '2':
            this.listQuery.childAdmin = this.searchTable
            this.getTabularData();
            break;
          case '3':
            this.listQuery.username = this.searchTable
            this.getTabularData();
            break;
        }
      },
      //编辑添加表单关闭事件
      addFormclose() {
        this.$nextTick(() => {
          this.$refs.ruleForm.resetFields();
          this.editSubjects = false
          this.addForm = this.$options.data().addForm
        })
      }
    }
  }
</script>
